from rest_framework.views import APIView
from rest_framework.permissions import IsAdminUser
from users.models import User
from django.utils import timezone
from rest_framework.response import Response


class UserTotalCountView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 统计网站总用户量
        count = User.objects.count()

        # 返回响应
        now_date = timezone.now()

        response_data = {
            'data': '',
            'count': count
        }

        return Response(response_data)


class UserDayIncrementView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 1.统计网站日新增用户数量
        now_date = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0)
        count = User.objects.filter(date_joined__gte=now_date)

        # 2.返回响应
        request_data = {
            'date': now_date.date(),
            'count': count,
        }

        return Response(response_data)


class UserDayActiveView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):

        # 1.统计网站当日活跃用户数量
        now_date = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0)
        count = User.objects.filter(last_login__gre=now_date).count()

        # 返回响应
        response_data = {
            'date': now_date.date(),
            'count': count,
        }
        return Response(response_data)